package com.ruoyi.supermarket.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supermarket.domain.*;
import com.ruoyi.supermarket.domain.Vo.ShXs;
import com.ruoyi.supermarket.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.service.ISaleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售汇总Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-28
 */
@Service
public class SaleServiceImpl implements ISaleService 
{
    @Autowired
    private SaleMapper saleMapper;

    @Autowired
    private XqsaleMapper xqsaleMapper;

    @Autowired
    private YspayMapper yspayMapper;

    @Autowired
    private XqyspayMapper xqyspayMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private QkMapper qkMapper;

    /**
     * 查询销售汇总
     * 
     * @param saleId 销售汇总主键
     * @return 销售汇总
     */
    @Override
    public Sale selectSaleBySaleId(Long saleId)
    {
        return saleMapper.selectSaleBySaleId(saleId);
    }

    /**
     * 查询销售汇总列表
     * 
     * @param sale 销售汇总
     * @return 销售汇总
     */
    @Override
    public List<Sale> selectSaleList(Sale sale)
    {
        List<String> list = new ArrayList<String>();
        list.add("0");
        list.add("2");
        list.add("4");
        sale.setIsRkList(list);
        return saleMapper.selectSaleList(sale);
    }

    /**
     * 新增销售汇总
     * 
     * @param sale 销售汇总
     * @return 结果
     */
    @Override
    public int insertSale(Sale sale)
    {
        sale.setCreateTime(LocalDateTime.now());
        return saleMapper.insertSale(sale);
    }

    /**
     * 修改销售汇总
     * 
     * @param sale 销售汇总
     * @return 结果
     */
    @Override
    public int updateSale(Sale sale)
    {
        return saleMapper.updateSale(sale);
    }

    /**
     * 批量删除销售汇总
     * 
     * @param saleIds 需要删除的销售汇总主键
     * @return 结果
     */
    @Override
    public int deleteSaleBySaleIds(Long[] saleIds)
    {
        return saleMapper.deleteSaleBySaleIds(saleIds);
    }

    /**
     * 删除销售汇总信息
     * 
     * @param saleId 销售汇总主键
     * @return 结果
     */
    @Override
    public int deleteSaleBySaleId(Long saleId)
    {
        return saleMapper.deleteSaleBySaleId(saleId);
    }

    @Override
    public AjaxResult audit(ShXs shXs) {
        Sale sale = saleMapper.selectSaleBySaleId(shXs.getSaleId());
        sale.setIsSh(shXs.getIsSh());
        sale.setShName(SecurityUtils.getLoginUser().getUsername());
        sale.setShTime(LocalDateTime.now());
        sale.setShRemark(shXs.getShRemark());
        saleMapper.updateSale(sale);
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult pay(List<Long> ids) {

        //只能是同一个客户
        Long supplyId =saleMapper.selectSaleBySaleId(ids.get(0)).getSupplyId();
        Long z_sum = 0L;
        BigDecimal z_price = BigDecimal.ZERO;
        for (Long id : ids) {
            Sale sale = saleMapper.selectSaleBySaleId(id);
            sale.setIsZd("1");
            saleMapper.updateSale(sale);
            z_sum += sale.getZnum();
            z_price = z_price.add(sale.getZprice());
        }
        //插入汇总
        Yspay yspay = new Yspay();
        yspay.setSupplyId(supplyId);
        yspay.setZsum(z_sum);
        yspay.setZprice(z_price);
        yspay.setCreateTime(LocalDateTime.now());
        yspay.setCreateName(SecurityUtils.getLoginUser().getUsername());
        yspay.setIsSh("1");
        yspay.setIsHx("0");
        yspayMapper.insertYspay(yspay);

        SimpleDateFormat dateFormat = new SimpleDateFormat("HHmmss");
        String dateTime = dateFormat.format(new Date());
        Long ysId = yspay.getYspayId();
        yspay.setYsCode("YS_"+dateTime +"_"+ysId);
        yspayMapper.updateYspay(yspay);

        //插入详情
        for (Long id : ids) {
            Xqsale xqsale = new Xqsale();
            xqsale.setSaleId(id);
            List<Xqsale> list = xqsaleMapper.selectXqsaleList(xqsale);
            for (Xqsale x : list) {
                Xqyspay xqyspay = new Xqyspay();
                xqyspay.setYspayId(ysId);
                xqyspay.setpId(x.getXqsaleId());
                xqyspayMapper.insertXqyspay(xqyspay);
            }
        }
        return AjaxResult.success(yspay.getYsCode());
    }

    @Override
    public AjaxResult outStore(Long saleId) {
        Sale sale = saleMapper.selectSaleBySaleId(saleId);
        sale.setIsRk("3");
        saleMapper.updateSale(sale);
        Xqsale x = new Xqsale();
        x.setSaleId(saleId);
        List<Xqsale> list = xqsaleMapper.selectXqsaleList(x);
        for (Xqsale xq : list) {
           Stock stock = stockMapper.selectStockBatch(xq.getBatch());
           Sale s = saleMapper.selectSaleBySaleId(saleId);
           Qk qk = qkMapper.selectBySupplyId(s.getSupplyId());
           qk.setQkPrice(qk.getQkPrice().add(xq.getZprice()));
           qkMapper.update(qk);
           stock.setStockNum(stock.getStockNum() - xq.getSum());
           stock.setStockPrice(stock.getStockPrice().add(stock.getCjPrice().multiply(BigDecimal.valueOf(xq.getSum()* -1))));
           stockMapper.updateStock(stock);
        }
        return AjaxResult.success();
    }

    @Override
    public List<Sale> selectSaleListTwo(Sale sale) {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("3");
        sale.setIsRkList(list);
        sale.setIsZd("0");
        return saleMapper.selectSaleList(sale);
    }

    @Override
    @Transactional
    public AjaxResult inStore(Long saleId) {
        Sale sale = saleMapper.selectSaleBySaleId(saleId);
        sale.setIsRk("1");
        saleMapper.updateSale(sale);
        Xqsale xq = new Xqsale();
        xq.setSaleId(saleId);
        List<Xqsale> list = xqsaleMapper.selectXqsaleList(xq);
        for (Xqsale xqsale : list) {
            xqsale.setIsRk("1");
            xqsaleMapper.updateXqsale(xqsale);
            Sale s = saleMapper.selectSaleBySaleId(saleId);
            Qk qk = qkMapper.selectBySupplyId(s.getSupplyId());
            qk.setQkPrice(qk.getQkPrice().add(sale.getZprice()));
            qkMapper.update(qk);
            Stock stock = stockMapper.selectStockBatch(xqsale.getBatch());

            stock.setStockNum(stock.getStockNum() - xqsale.getSum());
            stock.setStockPrice(stock.getStockPrice().add(stock.getCjPrice().multiply(BigDecimal.valueOf(xqsale.getSum()* -1 ))));
            // 库存总金额
            stockMapper.updateStock(stock);
        }
        return AjaxResult.success();
    }

    @Override
    public List<Sale> selectSaleListThree(Sale sale) {
        List<String> list = new ArrayList<String>();
        list.add("0");
        list.add("2");
        sale.setIsRkList(list);
        return saleMapper.selectSaleList(sale);
    }

    @Override
    public int updatefsh(Sale sale) {
        sale.setIsSh("1");
        return saleMapper.updateSale(sale);
    }

    @Override
    public List<Sale> selectSaleListZf(Sale sale) {
        List<String> list = new ArrayList<String>();
        list.add("0");
        list.add("2");
        sale.setIsRkList(list);
        List<String> listSh = new ArrayList<String>();
        listSh.add("1");
        listSh.add("2");
        sale.setIsShList(listSh);
        return saleMapper.selectSalelistf(sale);
    }

    @Override
    public AjaxResult zfSale(Long saleId) {
        saleMapper.deleteSaleBySaleId(saleId);
        xqsaleMapper.deleteXqsaleBySaleId(saleId);
        return null;
    }

    @Override
    public List<Sale> selectSaleListCs(Sale sale) {
        return saleMapper.selectSaleList(sale);
    }
}
